package com.camic.server.tool
{
	import com.camic.vo.WeatherVo;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;

	/**
	 * 本方法，主要用户获取指定城市的对应的天气
	 * @author Norris
	 */
	public class GetIpCityWeather
	{
		/** 成功的回调函数 **/
		private var m_fnSuccess:Function = null;
		/** 失败的回调函数 **/
		private var m_fnError:Function = null;
		/** 加载器 **/
		private var loader:URLLoader = null;
		/** 获取城市天气的Web服务网址 **/
		private const weaeherUrl:String = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName";
		/** GetIpCityWeather类的唯一实例 **/
		static private var m_instance:GetIpCityWeather = null;
		
		/**
		 * 获取GetIpCityWeather类的唯一实例
		 * @return GetIpCityWeather
		 */		
		static public function getInstance():GetIpCityWeather{
			if( m_instance == null ){
				m_instance = new GetIpCityWeather();
			}
			return m_instance;
		}
		
		public function GetIpCityWeather()
		{
		}
		
		public function _get( _cityName:String, __fnSuccess:Function, __fnError:Function = null ):void{
			m_fnSuccess = __fnSuccess;
			m_fnError = __fnError;
			loader = new URLLoader();
			addEvent();
			var _url:String = weaeherUrl + _cityName;
			var request:URLRequest=new URLRequest(weaeherUrl);
			request.method=URLRequestMethod.GET;
			request.data= "theCityName=" + _cityName;
			loader.load(request);
		}
		
		/**
		 * 添加监听事件 
		 */		
		private function addEvent():void{
			loader.addEventListener(Event.COMPLETE, parseResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		}
		
		/**
		 * 移除监听事件 
		 */		
		private function removeEvent():void{
			loader.removeEventListener(Event.COMPLETE, parseResult);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
		}
		
		/**
		 * 获取IP地址成功，对地址进行解释并发送相对应的事件通知 
		 * @param evt
		 */		
		private function parseResult(evt:Event):void{
			removeEvent();
			trace("--------------------------类：GetIpCityWeather，加载XML文件成功了--------------------------")
			var myCityXMl:XML=new XML(evt.target.data);
			putIntoVo(myCityXMl);
			// 发出通知，已经成功获取天气信息了.
			m_fnSuccess();
		}
		
		/**
		 * 将数据存入VO缓存器中 
		 * @param _vo XML
		 */		
		private function putIntoVo(_vo:XML):void {
			//  这样性能会更高效
			var weatherVo:WeatherVo= WeatherVo.getInstance();
			// 省
			weatherVo.province = _vo.children()[0].toString();
			// 市
			weatherVo.city = _vo.children()[1].toString();
			// 城市编码
			weatherVo.cityCode = _vo.children()[2].toString();
			// 更新时间
			weatherVo.updateTime = _vo.children()[4].toString();
			// 当前温度
			weatherVo.temperature = _vo.children()[5].toString();
			//  详细的天气描述
			weatherVo.weatherDescribe = _vo.children()[6].toString();
			// 风向
			weatherVo.windDirection = _vo.children()[7].toString();
			// 天气汇总
			weatherVo.summary = _vo.children()[10].toString();
			//  城市指数
			weatherVo.cityIndex = _vo.children()[11].toString();
			// 明天的温度
			weatherVo.tomorrowTemperature = _vo.children()[12].toString();
			// 明天天气
			weatherVo.tomorrowWeather = _vo.children()[13].toString();
			// 明天的风向
			weatherVo.tomorrowDescribe =  _vo.children()[14].toString();
			// 后天的温度
			weatherVo.afterTemperature = _vo.children()[17].toString();
			// 后天天气
			weatherVo.afterWeather = _vo.children()[18].toString();
			// 后天的风向
			weatherVo.afterDescribe =  _vo.children()[19].toString();
			// 关于城市
			weatherVo.aboutCity = _vo.children()[22].toString();
		}
		
		/**
		 * 加载XML文件出错的处理方法 
		 * @param e
		 */		
		private function errorHandler(e:IOErrorEvent):void{
			trace("--------------------------类：GetIpCityWeather，加载XML文件失败了--------------------------")
			removeEvent();
			destroy();
			// 发出通知，获取天气信息失败了
			m_fnError();
		}
		
		/**
		 * 销毁资源 
		 */		
		private function destroy():void{
			loader = null;
		}
	}
}